var gulp = require('gulp'),
    gutil = require('gulp-util'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
    concat = require('gulp-concat'),
    imagemin = require('gulp-imagemin'),
    gzip = require('gulp-gzip'),
    html2js = require('gulp-html2js');

// html2js
gulp.task('html.js', function() {
    gulp.src(['app/**/*.html'])
        .pipe(html2js('templates.js', {
            adapter: 'angular',
            base: 'templates',
            name: 'main'
        }))
        .pipe(gulp.dest('public/'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify())
        .pipe(gulp.dest('public/'));
});

gulp.watch(['app/**/*.html'], ['html.js']);

gulp.task('Controllerjs', function() {
    return gulp.src('app/pages/*/*.js')
        .pipe(concat('controllers.js'))
        .pipe(gulp.dest('public/js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify().on('error', gutil.log))
        .pipe(gulp.dest('public/js'));
});

gulp.watch(['app/pages/*/*.js'], ['Controllerjs']);


//mini directives js
gulp.task('Tpljs', function() {
    return gulp.src(['app/pages/*/*/*.js','app/tpls/*/*.js'])
        .pipe(concat('directives.js'))
        .pipe(gulp.dest('public/js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify().on('error', gutil.log))
        .pipe(gulp.dest('public/js'));
});

gulp.watch(['app/pages/*/*/*.js','app/tpls/*/*.js'], ['Tpljs']);


gulp.task('img', function() {
    return gulp.src('./assets/img/*')
        .pipe(imagemin())
        .pipe(gulp.dest('./gzip'));
});

gulp.task('gzip', function() {
    gulp.src('./public/**/*.js')
        .pipe(gzip())
        .pipe(gulp.dest('./gzip/js'));
});

//默认命令，在cmd中输入gulp
gulp.task('default', function() {
    //gulp.start('img');
    gulp.start(['html.js', 'Controllerjs', 'Tpljs']);
});
